*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

* Authors: Ranjit Lall & David Vilalta

* Paper: Varieties of Anxieties: Disaggregating Emotion and Voting Behavior in the COVID-19 Era

* Purpose: Cleans and formats dataset from CIS surveys

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

clear all 

* Insert folder path
global folder ".../Insert folder path"


* Set working directory for data files
cd "${folder}"

* Define local folders
global dta "Final_data/"
global tables "Results/Tables/"
global figures "Results/Figures/"
global interm_files "Raw_data/Intermediate_files/CIS_Survey/"
global cis_files "Raw_data/CIS_Survey/"


** Merging the data

use ${cis_files}avg_mun_cases_Catalonia, clear

forvalues i=1/7{
    if `i'==1 local sub="Navarra"
    if `i'==2 local sub="IB"
    if `i'==3 local sub="PV"
    if `i'==4 local sub="CyL"
    if `i'==5 local sub="CAM"
    if `i'==6 local sub="CVal"
    if `i'==7 local sub="Cantabria"

    append using ${cis_files}avg_mun_cases_`sub'
}
save ${interm_files}avg_mun_cases, replace

use ${cis_files}sum_mun_cases_Catalonia, clear

forvalues i=1/7{
    if `i'==1 local sub="Navarra"
    if `i'==2 local sub="IB"
    if `i'==3 local sub="PV"
    if `i'==4 local sub="CyL"
    if `i'==5 local sub="CAM"
    if `i'==6 local sub="CVal"
    if `i'==7 local sub="Cantabria"

    append using ${cis_files}sum_mun_cases_`sub'
}
save ${interm_files}sum_mun_cases, replace

use ${cis_files}cumulative_cases_Catalonia2, clear

forvalues i=1/7{
    if `i'==1 local sub="Navarra"
    if `i'==2 local sub="IB"
    if `i'==3 local sub="PV"
    if `i'==4 local sub="CyL"
    if `i'==5 local sub="CAM"
    if `i'==6 local sub="CVal"
    if `i'==7 local sub="Cantabria"

    append using ${cis_files}cumulative_cases_`sub'
}
save ${interm_files}cumulative_cases, replace



use ${cis_files}3X202105, clear

append using ${cis_files}3X202104
append using ${cis_files}3X202103
append using ${cis_files}3X202102
append using ${cis_files}3X202101
append using ${cis_files}3X202012
append using ${cis_files}3X202011
append using ${cis_files}3X202010
append using ${cis_files}3X202009
append using ${cis_files}3X202007
append using ${cis_files}3X202006
append using ${cis_files}3X202005
append using ${cis_files}3X202004
append using ${cis_files}3X202003
append using ${cis_files}3X202002
append using ${cis_files}3X202001
append using ${cis_files}3X201912
append using ${cis_files}3X201911
append using ${cis_files}3X201910
append using ${cis_files}3X201909
append using ${cis_files}3X201907
append using ${cis_files}3X201906
append using ${cis_files}3X201905
append using ${cis_files}3X201904
append using ${cis_files}3X201903
append using ${cis_files}3X201902
append using ${cis_files}3X201901
append using ${cis_files}3X201812
append using ${cis_files}3X201811
append using ${cis_files}3X201810
append using ${cis_files}3X201809
append using ${cis_files}3X201807
append using ${cis_files}3X201806


keep CCAA PROV MUN modate vox pp psoe podemos ciudadanos worr studies class laboursit civilstatus agegroup sex nationality jobtype sitecongen siteconpers covidknowledge covidinterest covidnewstrust covidinformed moreinfo neededmesures strictermesures confinementagree fakenews trustgov ccaagov optimism confinementfeeling civic decider oppositioncovid econconseq prolongue univwage prolongueconfin timeprolongueconfin covideffects changeshoppinghabits shoppingestablishmentpre shoppingestablishmentpost consumptionexpectations otherconsumptionexpectationsgen otherconsumptionexpectations contacthealth confined test covidpositive symptoms holidays holidaystime holidaysdest toughermesures schooling vaccine voxprev ppprev psoeprev podemosprev ciudadanosprev personaleffect socialeffect left_right_scale party_sympathy
save ${interm_files}conscis, replace

use ${cis_files}2020_mobility_rec, clear
append using ${cis_files}2021_mobility_rec
save ${interm_files}mobility_rec, replace

use ${cis_files}2020_mobility_rec_prov, clear
append using ${cis_files}2021_mobility_rec_prov
save ${interm_files}mobility_rec_prov, replace

use ${cis_files}2020_mobility_work, clear
append using ${cis_files}2021_mobility_work
save ${interm_files}mobility_work, replace

use ${cis_files}2020_mobility_work_prov, clear
append using ${cis_files}2021_mobility_work_prov
save ${interm_files}mobility_work_prov, replace

use ${interm_files}conscis, clear


rename modate date
merge m:1 PROV date using ${interm_files}mobility_rec_prov
drop _merge
merge m:1 PROV date using ${interm_files}mobility_work_prov
drop _merge
merge m:1 CCAA date using ${interm_files}mobility_rec
drop _merge
merge m:1 CCAA date using ${interm_files}mobility_work
drop _merge
merge m:1 PROV date using ${cis_files}avg_cases
drop _merge
merge m:1 PROV date using ${cis_files}sum_cases
drop _merge
merge m:1 PROV date using ${cis_files}avg_hosp
drop _merge
merge m:1 PROV date using ${cis_files}sum_hosp
drop _merge
merge m:1 PROV date using ${cis_files}avg_icu
drop _merge
merge m:1 PROV date using ${cis_files}sum_icu
drop _merge
merge m:1 PROV date using ${cis_files}avg_death
drop _merge
merge m:1 PROV date using ${cis_files}sum_death
drop _merge
merge m:1 PROV date using ${cis_files}cumcasesprov_2
drop _merge
merge m:1 PROV MUN date using ${cis_files}sum_mun_cases
drop _merge
merge m:1 PROV MUN date using ${cis_files}avg_mun_cases
drop _merge
merge m:1 PROV MUN date using ${cis_files}cumulative_cases
drop _merge
merge m:1 PROV date using ${cis_files}importsdata
drop _merge
merge m:1 PROV date using ${cis_files}employmentdata
drop _merge
merge m:1 PROV date using ${cis_files}unempdata
drop _merge
merge m:1 PROV date using ${cis_files}avgunempdata
drop _merge
merge m:1 PROV date using ${cis_files}avggdpgrowth
drop _merge
merge m:1 PROV MUN using ${cis_files}population
drop _merge
merge m:1 PROV using ${cis_files}provpopulation
drop _merge
save ${interm_files}consdata.dta, replace
merge m:1 PROV MUN using ${cis_files}nursingdata
drop _merge

drop if CCAA==.



**CCAA/PROV names


rename CCAA ccaa
rename PROV prov
rename MUN mun

gen provname="A"
replace provname="Araba/Álava" if prov==1
replace provname="Albacete" if prov==2
replace provname="Alacant/Alicante" if prov==3
replace provname="Almería" if prov==4
replace provname="Ávila" if prov==5
replace provname="Badajoz" if prov==6
replace provname="Illes Balears" if prov==7
replace provname="Barcelona" if prov==8
replace provname="Burgos" if prov==9
replace provname="Cáceres" if prov==10
replace provname="Cádiz" if prov==11
replace provname="Castelló/Castellón" if prov==12
replace provname="Ciudad Real" if prov==13
replace provname="Córdoba" if prov==14
replace provname="Coruña (A)" if prov==15
replace provname="Cuenca" if prov==16
replace provname="Girona" if prov==17
replace provname="Granada" if prov==18
replace provname="Guadalajara" if prov==19
replace provname="Gipuzkoa" if prov==20
replace provname="Huelva" if prov==21
replace provname="Huesca" if prov==22
replace provname="Jaén" if prov==23
replace provname="León" if prov==24
replace provname="Lleida" if prov==25
replace provname="Rioja (La)" if prov==26
replace provname="Lugo" if prov==27
replace provname="Madrid" if prov==28
replace provname="Málaga" if prov==29
replace provname="Murcia" if prov==30
replace provname="Navarra" if prov==31
replace provname="Ourense" if prov==32
replace provname="Asturias" if prov==33
replace provname="Palencia" if prov==34
replace provname="Palmas (Las)" if prov==35
replace provname="Pontevedra" if prov==36
replace provname="Salamanca" if prov==37
replace provname="Santa Cruz de Tenerife" if prov==38
replace provname="Cantabria" if prov==39
replace provname="Segovia" if prov==40
replace provname="Sevilla" if prov==41
replace provname="Soria" if prov==42
replace provname="Tarragona" if prov==43
replace provname="Teruel" if prov==44
replace provname="Toledo" if prov==45
replace provname="València/ Valencia" if prov==46
replace provname="Valladolid" if prov==47
replace provname="Bizkaia" if prov==48
replace provname="Zamora" if prov==49
replace provname="Zaragoza" if prov==50
replace provname="Ceuta" if prov==51
replace provname="Melilla" if prov==52

gen ccaaname="A"
replace ccaaname="Andalucía" if ccaa==1
replace ccaaname="Aragón" if ccaa==2
replace ccaaname="Asturias (Principado de)" if ccaa==3
replace ccaaname="Balears (Illes)" if ccaa==4
replace ccaaname="Canarias" if ccaa==5
replace ccaaname="Cantabria" if ccaa==6
replace ccaaname="Castilla La Mancha" if ccaa==7
replace ccaaname="Castilla y León" if ccaa==8
replace ccaaname="Cataluña" if ccaa==9
replace ccaaname="Comunitat Valenciana" if ccaa==10
replace ccaaname="Extremadura" if ccaa==11
replace ccaaname="Galicia" if ccaa==12
replace ccaaname="Madrid (Comunidad de)" if ccaa==13
replace ccaaname="Murcia (Región de)" if ccaa==14
replace ccaaname="Navarra (Comunidad Foral de)" if ccaa==15
replace ccaaname="País Vasco" if ccaa==16
replace ccaaname="Rioja (La)" if ccaa==17
replace ccaaname="Ceuta (Ciudad autónoma de)" if ccaa==18
replace ccaaname="Melilla (Ciudad autónoma de)" if ccaa==19

drop if ccaa==18
drop if ccaa==19
drop if provname=="A"


order ccaa prov mun provname date vox pp psoe podemos ciudadanos worr agegroup sex nationality civilstatus laboursit class studies

sort date prov

xtset prov

gen chinaexp=(indemployed/natindemployed)*(Dmanimpchina/totalemployed)

gen govt=0
replace govt=1 if psoe==1 | podemos==1
replace govt=. if pp==.
gen nogovt=1
replace nogovt=. if pp==. 
replace nogovt=0 if psoe==1 | podemos==1

gen prevgovt=0
replace prevgovt=1 if psoeprev==1 | podemosprev==1
replace prevgovt=. if ppprev==.
gen prevnogovt=1
replace prevnogovt=0 if psoeprev==1 | podemosprev==1
replace prevnogovt=. if ppprev==.

replace avg_hosp=0 if avg_hosp==. & date < tm(2020m3)
replace sum_hosp=0 if sum_hosp==. & date < tm(2020m3)
replace avg_icu=0 if avg_icu==. & date < tm(2020m3)
replace sum_icu=0 if sum_icu==. & date < tm(2020m3)
replace avg_death=0 if avg_death==. & date < tm(2020m3)
replace sum_death=0 if sum_death==. & date < tm(2020m3)

replace nursinghomes=0 if mun!=0 & date < tm(2020m3)
replace nursingplaces=0 if mun!=0 & date < tm(2020m3)

replace avg_cases=0 if avg_cases==. & date < tm(2020m3)
replace sum_cases=0 if sum_cases==. & date < tm(2020m3)
replace avg_mun_cases=0 if avg_mun_cases==. & mun!=0 & date < tm(2020m3)
replace sum_mun_cases=0 if sum_mun_cases==. & mun!=0 & date < tm(2020m3)
replace cumulative_cases=0 if cumulative_cases==. & mun!=0 & date < tm(2020m3)
replace cumulative_cases_prov=0 if cumulative_cases_prov==. & date < tm(2020m3)
replace num_casos=0 if num_casos==. & date < tm(2020m3)
replace num_hosp=0 if num_hosp==. & date < tm(2020m3)
replace num_uci=0 if num_uci==. & date < tm(2020m3)
replace num_def=0 if num_def==. & date < tm(2020m3)


rename num_casos num_casos_p
rename num_hosp num_hosp_p
rename num_uci num_uci_p
rename num_def num_def_p

rename avg_cases avg_cases_p
rename sum_cases sum_cases_p
rename cumulative_cases_prov cumulative_cases_p
rename avg_mun_cases avg_cases_m
rename sum_mun_cases sum_cases_m
rename cumulative_cases cumulative_cases_m

replace num_casos_p=0 if num_casos_p==. & date < tm(2020m3)
replace avg_cases_p=0 if avg_cases_p==. & date < tm(2020m3)
replace sum_cases_p=0 if sum_cases_p==. & date < tm(2020m3)
replace cumulative_cases_p=0 if cumulative_cases_p==. & date < tm(2020m3)
replace avg_cases_m=0 if avg_cases_m==. & mun!=0 & date < tm(2020m3)
replace sum_cases_m=0 if sum_cases_m==. & mun!=0 & date < tm(2020m3)
replace cumulative_cases_m=0 if cumulative_cases_m==. & mun!=0 & date < tm(2020m3)

rename POB20 pob_mun

gen num_casos_p_P=num_casos_p/pop_prov*100
replace num_casos_p_P=0 if num_casos_p_P==. & date < tm(2020m3)

gen avg_cases_p_P=avg_cases_p/pop_prov*100
gen sum_cases_p_P=sum_cases_p/pop_prov*100
gen cumulative_cases_p_P=cumulative_cases_p/pop_prov*100
gen avg_cases_m_P=avg_cases_m/pob_mun*100
gen sum_cases_m_P=sum_cases_m/pob_mun*100
drop cumulative_cases_m_P
gen cumulative_cases_m_P=cumulative_cases_m/pob_mun*100


forvalues i=1/5{
if `i'==1 local sub="1"
if `i'==2 local sub="2"
if `i'==3 local sub="3"
if `i'==4 local sub="4"
if `i'==5 local sub="5"

gen laboursit`sub'=0
replace laboursit`sub'=`sub' if laboursit==`sub'
}

forvalues i=1/11{
if `i'==1 local sub="1"
if `i'==2 local sub="2"
if `i'==3 local sub="3"
if `i'==4 local sub="4"
if `i'==5 local sub="5"
if `i'==6 local sub="6"
if `i'==7 local sub="7"
if `i'==8 local sub="8"
if `i'==9 local sub="9"
if `i'==10 local sub="10"
if `i'==11 local sub="11"

gen jobtype`sub'=0
replace jobtype`sub'=1 if jobtype==`sub'
}

forvalues i=1/4{
if `i'==1 local sub="1"
if `i'==2 local sub="2"
if `i'==3 local sub="3"
if `i'==4 local sub="4"

gen covideffects`sub'=.
replace covideffects`sub'=1 if covideffects==`sub'
}
replace covideffects1=0 if covideffects2==1
replace covideffects2=0 if covideffects1==1

gen covideffectsb=0 if covideffects==2
replace covideffectsb=0.5 if covideffects==3
replace covideffectsb=1 if covideffects==1


gen date2=date
format date2 %tm

replace date = dofm(date)
replace date = cofd(date) + tc(00:00:35)

*drop if mun==0

drop mun

encode NOMBRE, gen(mun)

gen nursinghomes_P=nursinghomes/pob_mun
gen nursingplaces_P=nursingplaces/pob_mun
gen log_nursingplaces=log(nursingplaces+1)


gen jobtype_2=1 if jobtype==1 
replace jobtype_2=2 if jobtype==2 | jobtype==3
replace jobtype_2=3 if jobtype==4
replace jobtype_2=4 if jobtype==5
replace jobtype_2=5 if jobtype==6
replace jobtype_2=6 if jobtype==7 | jobtype==8 
replace jobtype_2=7 if jobtype==9 | jobtype==10 | jobtype==11 

rename jobtype jobtype_old
rename jobtype_2 jobtype


replace class=5 if class==6



gen studies_2=1 if studies==1
replace studies_2=2 if studies==2
replace studies_2=3 if studies==3 | studies==4 | studies==5
replace studies_2=4 if studies==6

rename studies studies_old
rename studies_2 studies

gen proconfprev=0
replace proconfprev=1 if psoeprev==1 | podemosprev==1 | ciudadanosprev==1

gen proconf=0
replace proconf=1 if psoe==1 | podemos==1 | ciudadanos==1

gen anticonfprev=0
replace anticonfprev=1 if pp==1 | vox==1

gen anticonf=0
replace anticonf=1 if ppprev==1 | voxprev==1

rename num_casos_p new_cases

keep proconf anticonf podemos psoe ciudadanos pp vox proconfprev anticonfprev podemosprev psoeprev ciudadanosprev ppprev voxprev covideffects1 covideffects2 covideffectsb worr jobtype jobtype_old agegroup sex class studies laboursit ccaa date2 prov cumulative_cases_p_P symptoms new_cases left_right_scale party_sympathy

gen ln_num_casos_p_P=log(cumulative_cases_p_P+1)

gen ccaa_date=ccaa*date2

gen prov_date=prov*date2

gen dummy=sex
drop sex
gen sex=0 if dummy!=.
replace sex=1 if dummy==0
drop dummy

encode party_sympathy, gen(party_sympathy_num)

label var worr "General COVID-19 Worry"
label var agegroup "Age Group"
label var sex "Gender: Female"
label var class "Social Class"
label var symptoms "COVID-19 Symptoms"
label var covideffectsb "Worried: Health vs. Economy"
label var jobtype "Job Type"
label var laboursit "Labour Situation"
label var studies "Level of Studies"
label var covideffects1 "Worried - health"
label var covideffects2 "Worried - economy"
label var worr "Worried"
label var vox "Would vote for Vox"
label var pp "Would vote for PP"
label var psoe "Would vote for PSOE"
label var podemos "Would vote for Podemos"
label var ciudadanos "Would vote for Ciudadanos"
label var voxprev  "Previous vote - Vox"
label var ppprev  "Previous vote - PP"
label var psoeprev "Previous vote - PSOE"
label var podemosprev "Previous vote - Podemos"
label var ciudadanosprev "Previous vote - Ciudadanos"
label var jobtype_old "Job Type (Old)"
label var party_sympathy "Party Sympathy"
label var cumulative_cases_p_P "Cumulative Cases per person"
label var date2 "Date"
label var proconfprev "Previous vote - Pro-confinement"
label var proconf "Would vote for Pro-confinement"
label var anticonfprev "Previous vote - Anti-confinement"
label var anticonf  "Would vote for Anti-confinement"
label var ln_num_casos_p_P "Cumulative Cases per person (logged)"
label var ccaa_date "CCAA x Date"
label var prov_date "Province x Date"
label var party_sympathy_num "Party Sympathy (num)"

rename ciudadanos cs
rename podemos pod

rename ciudadanosprev csprev
rename podemosprev podprev


save ${dta}Finaldata_CIS, replace
